package cn.mineserv.video.videoordersystem.controller;

import cn.mineserv.video.videoordersystem.model.Comment;
import cn.mineserv.video.videoordersystem.model.Media;
import cn.mineserv.video.videoordersystem.model.User;
import cn.mineserv.video.videoordersystem.service.CategoryService;
import cn.mineserv.video.videoordersystem.service.CommentService;
import cn.mineserv.video.videoordersystem.service.MediaService;
import cn.mineserv.video.videoordersystem.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/media")
public class MediaController {
    @Resource
    private MediaService mediaService;
    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;
    @Resource
    private CategoryService categoryService;

    @GetMapping("/detail")
    public String movieDetails(Model model, @RequestParam(defaultValue = "1") Integer id){
        Media media = mediaService.getById(id);
        if(media == null){
            media = mediaService.getById(1);
        }
        User user = userService.getById(media.getUserId());
        model.addAttribute("media",media);
        model.addAttribute("user",user);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_id",id);
        List<Comment> commentList = commentService.list(queryWrapper);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("media_id",id);

        List<Comment> list =  commentService.list(wrapper);
        model.addAttribute("comment",list);
        model.addAttribute("commentList",commentList);
        return "/media/movie/MovieDetail";
    }

    @PostMapping("/comment")
    public String comment(Model model,Integer userId ,Integer mediaId,String comment){
        Comment commentObj =  new Comment();
        commentObj.setUserId(userId);
        commentObj.setMediaId(mediaId);
        commentObj.setCommentText(comment);
        commentObj.setCommentLike(1);
        commentObj.setCommentTime(new Date());
        commentService.save(commentObj);

//        model.addAttribute("comment",commentObj);
        return "redirect:/media/detail?id="+mediaId;
    }

    @GetMapping("/play")
    public String playVideo(Model model, Integer id){
        Media media = mediaService.getById(id);
        model.addAttribute("media", media);
        return "/media/video/PlayVideo";
    }
    @GetMapping("/upload")
    public String uploadVideo(Model model, Integer id){

        model.addAttribute("cate", categoryService.list());
        return "/media/video/UpdateVideo";
    }

    @GetMapping("/download")
    public void downloadVideo(HttpServletResponse response, Integer mediaId) throws Exception{
        Media media = mediaService.getById(mediaId);
        response.sendRedirect(media.getMediaDownload());
    }

}
